package com.moon.icoresso.strategy.impl;

import com.moon.icoresso.Exception.OauthException;
import com.moon.icoresso.Service.OauthRedisService;
import com.moon.icoresso.Service.OauthService;
import com.moon.icoresso.common.utils.StringRedisService;
import com.moon.icoresso.pojo.bo.OauthTokenHandleBO;
import com.moon.icoresso.pojo.dto.OauthTokenDTO;
import com.moon.icoresso.pojo.dto.OauthTokenParam;
import com.moon.icoresso.pojo.dto.OauthUserInfo;
import com.moon.icoresso.pojo.dto.UserDTO;
import com.moon.icoresso.strategy.OauthStrategy;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.moon.icoresso.pojo.constants.Constants4Oauth.MAX_EXPIRE_TIME;
import static com.moon.icoresso.pojo.constants.Constants4Oauth.OAUTH_PASSWORD_GRANT_TYPE;

/**
 * @ClassName OauthPasswordStrategy
 * @Description
 * @Author menbbo
 * @Date 2021/2/19 20:14
 * @Version V1.0
 **/
@Component(value = OAUTH_PASSWORD_GRANT_TYPE)
public class OauthPasswordStrategy implements OauthStrategy{
    @Autowired
    private OauthService oauthService;
    @Autowired
    private OauthRedisService oauthRedisService;
    @Autowired
    private StringRedisService<String,OauthUserInfo> stringVStringRedisService;
    @Override
    public void checkParam(OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO) {
        UserDTO userDTO = new UserDTO();
        userDTO.setClientId(oauthTokenParam.getClientId());
        userDTO.setRedirectUri(oauthTokenParam.getRedirectUri());
        oauthService.checkClientIdAndRedirectUri(userDTO);
        //校验用户名与密码
        userDTO.setUsername(oauthTokenParam.getUsername());
        userDTO.setPassword(oauthTokenParam.getPassword());
        oauthService.checkUserDTO(userDTO);
    }

    @Override
    public OauthTokenDTO handle(OauthTokenParam oauthTokenParam, OauthTokenHandleBO oauthTokenHandleBO) {
        UserDTO user = getUser(oauthTokenParam);
        if(user==null&&StringUtils.isEmpty(user.getUserId())){
            throw new OauthException("用名与密码不匹配");
        }
        //oauthTokenHandleBO可以模拟获取用户信息
        OauthUserInfo oauthUserInfo = new OauthUserInfo();
        oauthUserInfo.setUserName(oauthTokenParam.getUsername());
        oauthUserInfo.setEmail("142@163.com");
        oauthTokenHandleBO.setOauthUserInfo(oauthUserInfo);
        //保存oauthUserInfo至redis
        oauthTokenHandleBO.setUserInfoRedisKey(oauthRedisService.generateUserInfoRedisKey(oauthTokenParam.getUsername()));
        stringVStringRedisService.set(oauthTokenHandleBO.getUserInfoRedisKey(),oauthUserInfo,MAX_EXPIRE_TIME);

        OauthTokenDTO oauthTokenDTO = oauthService.generateToken(true);
        //将token保存
        oauthRedisService.saveAccessToken(oauthTokenParam.getGrantType(),oauthTokenParam.getClientId(),oauthTokenDTO.getAccessToken(),oauthTokenHandleBO.getOauthUserInfo());
        oauthRedisService.saveRefreshToken(oauthTokenDTO.getRefreshToken(),oauthTokenParam.getGrantType(),oauthTokenParam.getClientId(),oauthTokenHandleBO.getOauthUserInfo());
        return oauthTokenDTO;
    }

    private UserDTO getUser(OauthTokenParam oauthTokenParam){
        if(StringUtils.equalsIgnoreCase(oauthTokenParam.getUsername(),oauthTokenParam.getPassword())){
            UserDTO userDTO1 = new UserDTO();
            userDTO1.setUserId("zhangsan123");
            userDTO1.setUsername("张三");
            return userDTO1;
        }else {
            return null;
        }
    }
}